local Msg = require("core.Msg")
local DrawExcel = require("excel.OperateActivity").payDraw
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local MailLogicFL = require("mail.MailLogicFL")
local CowryBagLogic = require("fuli.CowryBagLogic")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local OperateActFL = require("activity.OperateActFL")
local HtmlUtil = require("common.HtmlUtil")
local Log = require("common.Log")
local ObjHuman = require("core.ObjHuman")

function open(human, id, type)
  local sendMsg = Msg.gc.GC_OPEN_YUNYING_PAYDRAW_ACTIVITY
  local activityNum
  if type == 2 then
    activityNum = OperateActFL.onGetTomorrowActRuleID(id)
  else
    activityNum = OperateActFL.onGetCurActRuleID(id)    
  end
  local activity = DrawExcel[activityNum]
  if not activity then
    return
  end
  sendMsg.info[0] = #activity.weight
  local cache = {}
  for i=1, #activity.weight do
    sendMsg.info[i].reward[0] = 1
    Grid.create(cache, activity.reward[i][1], activity.reward[i][2])
    Grid.makeItem(cache, sendMsg.info[i].reward[1], 0, human)
    sendMsg.info[i].args[0] = 0
    sendMsg.info[i].canGet = 0
    sendMsg.info[i].name = ""
  end
  sendMsg.myInfo[0] = 3
  sendMsg.myInfo[1] = activity.canDraw    --花费多少元宝可抽奖
  sendMsg.myInfo[2] = human.db.YYPayDrawTimes    --已抽奖次数
  sendMsg.myInfo[3] = activity.maxDraw    --最大抽奖次数
  sendMsg.activityId = id
  sendMsg.type = type or 1
  
  sendMsg.rewardInfo[0] = #activity.drawTimes
  for i=1, #activity.drawTimes do
    sendMsg.rewardInfo[i].drawTimes = activity.drawTimes[i]
    sendMsg.rewardInfo[i].status = human.db.YYPayDrawStatus[i - 1]
    Grid.create(cache, activity.freeReward[i][1], activity.freeReward[i][2])
    Grid.makeItem(cache, sendMsg.rewardInfo[i].reward, 0, human)
    sendMsg.rewardInfo[i].index = i
  end
  
  Msg.send(sendMsg, human.fd)
--  local info = {}
--  info.type = id
--  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_YUNYING_RECORD, info)  
end
--得到索引
function getIndex(human, activity, times)
  local weight = 0
  for i=1, #activity.weight do
    weight = weight + activity.weight[i]
  end
  local rand = math.random(1, weight)
  weight = 0
  for i=1, #activity.weight do
    weight = weight + activity.weight[i]
    if rand <= weight then
      
      human.db.YYPayDrawIndex[times - 1] = i
      break
    end
  end
end

--开始抽奖
function startDraw(human, times)
  local activityNum = OperateActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_PAYDRAW)
  local activity = DrawExcel[activityNum]
  if not activity then
    return
  end
  for i=1, 10 do
    human.db.YYPayDrawIndex[i-1] = 0
  end
  if human.db.YYPayDrawTimes + times  > activity.maxDraw then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_MAX_DRAW)
  end
  if ObjHuman.getYuanbao(human) < activity.canDraw * times then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_YUANBAO_NOT_EUNOUGH)
  end
  ObjHuman.decYuanbao(human, activity.canDraw * times, "yunyingAcvity")

  local sendMsg = Msg.gc.GC_YUNYING_PAYDRAW_GET_REWARD
  for i=1, times do
    getIndex(human, activity, i)
  end
  
  sendMsg.index = human.db.YYPayDrawIndex[0]
  sendMsg.reward[0] = times
  local cache = {} 
  for i=1, times do
    Grid.create(cache, activity.reward[human.db.YYPayDrawIndex[i -1] ][1], activity.reward[human.db.YYPayDrawIndex[i -1] ][2])
    Grid.makeItem(cache, sendMsg.reward[i], 0, human)
  end
  Msg.send(sendMsg, human.fd)
  human.db.YYPayDrawTimes = human.db.YYPayDrawTimes + times  
  
  local item = {}
  for i=1, 10 do    
    if human.db.YYPayDrawIndex[i - 1] ~= 0 then
      local isHave = false
      for j=1, #item do
        if item[j] and item[j][1] == activity.reward[human.db.YYPayDrawIndex[i - 1]][1] then
          item[j][2] = item[j][2] + activity.reward[human.db.YYPayDrawIndex[i - 1]][2]
          isHave = true
        end
      end
      if not isHave then
        item[#item + 1] = {}
        item[#item][1] = activity.reward[human.db.YYPayDrawIndex[i-1]][1]
        item[#item][2] = activity.reward[human.db.YYPayDrawIndex[i-1]][2]
      end
    end
  end
  if not BagLogic.checkCanAddItemList(human, item, #item) then
    sendMail(human, item)
    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH_SEND_MAIL)
  else
    for i=1, #item do
      BagLogic.addItem(human, item[i][1], 1, item[i][2], "yunyingAcvity")
    end
  end
  for i=1, 10 do
    human.db.YYPayDrawIndex[i - 1] = 0
  end
  for i=1, #activity.drawTimes do
    if human.db.YYPayDrawTimes >= activity.drawTimes[i] and human.db.YYPayDrawStatus[i - 1] == ActDefine.REWARD_NOT_CAN_GET then
      human.db.YYPayDrawStatus[i - 1] = ActDefine.REWARD_CAN_GET  
    end
  end
  open(human, ActDefine.OPERATEACT_TYPE_PAYDRAW) 
  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_DRAW_END)
  
  if #item == 1 then
    local cache = {}
    Grid.create(cache, item[1][1], item[1][2])
    local str = string.format(Lang.YUNYING_PAY_DRAW_BRO, HtmlUtil.fontName(human.name, human._id),  HtmlUtil.fontItem(cache), human._id)
    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  else
    local str = string.format(Lang.YUNYING_PAY_DRAW_BRO1, HtmlUtil.fontName(human.name, human._id), human._id)
    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  end
  Log.write(Log.LOGID_OSS_PAY_DRAW, human._id, human.account, human.name,human.db.lv, 1, times)
end

function endDraw(human)
--  if human.db.YYPayDrawIndex[0] == 0 then
--    return
--  end
--  local activityNum = OperateActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_PAYDRAW)
--  local activity = DrawExcel[activityNum]
--  if not activity then
--    return
--  end
--  local item = {}
--  for i=1, 10 do    
--    if human.db.YYPayDrawIndex[i - 1] ~= 0 then
--      local isHave = false
--      for j=1, #item do
--        if item[j] and item[j][1] == activity.reward[human.db.YYPayDrawIndex[i - 1]][1] then
--          item[j][2] = item[j][2] + activity.reward[human.db.YYPayDrawIndex[i - 1]][2]
--          isHave = true
--        end
--      end
--      if not isHave then
--        item[#item + 1] = {}
--        item[#item][1] = activity.reward[human.db.YYPayDrawIndex[i-1]][1]
--        item[#item][2] = activity.reward[human.db.YYPayDrawIndex[i-1]][2]
--      end
--    end
--  end
--  if not BagLogic.checkCanAddItemList(human, item, #item) then
--    sendMail(human, item)
--    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH_SEND_MAIL)
--  else
--    for i=1, #item do
--      BagLogic.addItem(human, item[i][1], 1, item[i][2], "yunyingAcvity")
--    end
--  end
--  for i=1, 10 do
--    human.db.YYPayDrawIndex[i - 1] = 0
--  end
--  for i=1, #activity.drawTimes do
--    if human.db.YYPayDrawTimes >= activity.drawTimes[i] and human.db.YYPayDrawStatus[i - 1] == ActDefine.REWARD_NOT_CAN_GET then
--      human.db.YYPayDrawStatus[i - 1] = ActDefine.REWARD_CAN_GET  
--    end
--  end
--  open(human, ActDefine.OPERATEACT_TYPE_PAYDRAW) 
--  Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_DRAW_END)
--  
--  if #item == 1 then
--    local cache = {}
--    Grid.create(cache, item[1][1], item[1][2])
--    local str = string.format(Lang.YUNYING_PAY_DRAW_BRO, HtmlUtil.fontName(human.name, human._id),  HtmlUtil.fontItem(cache), human._id)
--    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
--  else
--    local str = string.format(Lang.YUNYING_PAY_DRAW_BRO1, HtmlUtil.fontName(human.name, human._id), human._id)
--    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
--  end
end


function sendMail(human, item)
  local nGridList = {}
  local activityNum = OperateActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_PAYDRAW)
  local activity = DrawExcel[activityNum]
  if not activity then
    return
  end
  for i=1, #item do
    nGridList[i] = Grid.create(nil, item[i][1], item[i][2], 1)  
  end
  
  MailLogicFL.CreateNewMailForUser(human._id, "快乐抽奖", "由于您的背包已满，特将奖励发给您", nGridList)
end

function getReward(human, index)
  if human.db.YYPayDrawStatus[index - 1] ~= ActDefine.REWARD_CAN_GET then
    return
  end
  local activityNum = OperateActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_PAYDRAW)
  local activity = DrawExcel[activityNum]
  if not activity then
    return
  end
  if not BagLogic.checkCanAddItem(human, activity.freeReward[index][1], 1, activity.freeReward[index][2]) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH)
  end
  BagLogic.addItem(human, activity.freeReward[index][1], 1, activity.freeReward[index][2], "yunyingAcvity")
  
  human.db.YYPayDrawStatus[index - 1] = ActDefine.REWARD_ALREADY_GET
  
  open(human, ActDefine.OPERATEACT_TYPE_PAYDRAW) 
  
end







